/**
 * 
 */
package gui.views;

import gui.TypeListWidget;

import java.awt.BorderLayout;
import java.util.prefs.Preferences;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import util.ForwardingListModel;

/**
 * @author Joshua Lockerman
 *
 */
public class TypeInspector extends JFrame implements ListSelectionListener {
    
    private final JTextField typeName;
    private final JTextArea typeDescription;
    private final TypeListWidget types;
    private final JList<metagrammar.Type> superTypes;
    
    public TypeInspector()
    {
        super("Type Inspector");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLayout(new BorderLayout());
        JPanel textPanel = new JPanel();
        textPanel.setLayout(new BorderLayout());
        typeName = new JTextField(10);
        typeName.setEditable(false);
        textPanel.add(typeName, BorderLayout.NORTH);
        
        typeDescription = new JTextArea(5, 30);
        typeDescription.setEditable(false);
        typeDescription.setLineWrap(true);
        JScrollPane scroll = new JScrollPane(typeDescription,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        textPanel.add(scroll, BorderLayout.CENTER);
        
        superTypes = new JList<metagrammar.Type>();
        superTypes.setLayoutOrientation(JList.HORIZONTAL_WRAP);
        JScrollPane sp = new JScrollPane(superTypes, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        sp.add(superTypes);
        textPanel.add(sp, BorderLayout.SOUTH);
        
        add(textPanel, BorderLayout.CENTER);
        
        types = new TypeListWidget();
        types.addListSelectionListener(this);
        add(types, BorderLayout.EAST);
        
        pack();
        
        Preferences prefs = Preferences.userNodeForPackage(TypeInspector.class);
        int width = prefs.getInt("width", getWidth());
        int height = prefs.getInt("height", getHeight());
        setSize(width, height);
    }
    
    public TypeInspector(int tabIndex, int listIndex)
    {
        this();
        
        types.setSelection(tabIndex, listIndex);
    }
    
    @Override
    public void valueChanged(ListSelectionEvent e)
    {
        metagrammar.Type type = types.getSelectedType();
        typeName.setText(type.getName());
        setTitle("Type Inspector: " + type.getName());
        typeDescription.setText(type.getDescription());
        ListModel<metagrammar.Type> typeModel = new ForwardingListModel<metagrammar.Type>(
                type.getSuperTypes());
        superTypes.setModel(typeModel);
    }
    
    @Override
    public void dispose()
    {
        Preferences prefs = Preferences.userNodeForPackage(TypeInspector.class);
        prefs.putInt("width", getWidth());
        prefs.putInt("height", getHeight());
        super.dispose();
    }
}
